//
//  PainterKLine.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterKLine.h"

using namespace std;
using namespace ytx;

@implementation PainterKLine

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    KLineData * data = (KLineData *)indexPosition->getData();

    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    string color = "";
    CGFloat HighY, LowY, BeginY, EndY, hollowY, zoomX;
    CGFloat tempX = rect.getBeginX();
    CGFloat KX = tempX;
    CGFloat lineWidth = style.quoteLineWidth;
    CGFloat lineWidthHalf = lineWidth * 0.5f;

    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    //    int startIndex = data->getStartIndex() , showCount = data->getShowCount(), endIndex = this->data->getEndIndex();

    //    rect.setZoomX(showCount);

    zoomX = rect.getZoomX();

    tempX += zoomX / 2.0;

    [ctx setLineWidth:lineWidth ];
    KLineItem item;
    vector<KLineItem> & kData = data->kLineData;
    
    CGFloat maxHighY = 0, maxHighX = 0, minLowY = 0, minLowX = 0;
    double highMax = FLT_MIN, lowMin = FLT_MAX;
    
    for (vector<KLineItem>::iterator it = kData.begin() + startIndex; it != kData.begin() + endIndex; ++it) {
        item = (*it);

        if( !isnan(item.close) ){
            BeginY = rect.getPxY(item.close);
            EndY = rect.getPxY(item.open);
            HighY = rect.getPxY(item.high);
            LowY = rect.getPxY(item.low);

            if(item.open > item.close)
            {
                color = style.defaultGreen;
                hollowY = BeginY;
            }
            else if(item.open < item.close){
                color = style.defaultRed;
                hollowY = EndY;
            }
            else {
                color = style.defaultWhite;
                hollowY = EndY;
            }
            
            if (item.low < lowMin) {
                minLowY = LowY;
                minLowX = tempX;
                lowMin = item.low;
            }
            
            if (item.high > highMax) {
                maxHighY = HighY;
                maxHighX = tempX;
                highMax = item.high;
            }
            
            [ctx setStrokeStyle:[NSString stringWithUTF8String:color.c_str()]];
            [ctx drawLineWithX1:tempX withY1:HighY withX2:tempX withY2:LowY];
            [ctx fillRectLeftBottomWithX:KX+lineWidthHalf withY:hollowY withWidth:zoomX-lineWidth withHeight:fmax(1, fabs(EndY - BeginY)) withFillColor:[NSString stringWithUTF8String:color.c_str()]];

        }
        tempX += zoomX;
        KX += zoomX;
    }
    if (highMax != 0) {
        [ctx fillText: [NSString stringWithUTF8String:Tools::Convert2FixString(highMax, data->toFix).c_str()]  withX:maxHighX withY:maxHighY withColor:[NSString stringWithUTF8String:style.kLineHighLowColor.c_str()] withFont:[ NSString stringWithUTF8String:style.font.c_str() ] withSize:style.size withAnchor:[self highLowAnchorWithX:maxHighX withY:maxHighY withRect:&rect]];
    }
    if (lowMin != 0) {
        [ctx fillText: [NSString stringWithUTF8String:Tools::Convert2FixString(lowMin, data->toFix).c_str()]  withX:minLowX withY:minLowY withColor:[NSString stringWithUTF8String:style.kLineHighLowColor.c_str()] withFont:[ NSString stringWithUTF8String:style.font.c_str() ] withSize:style.size withAnchor:[self highLowAnchorWithX:minLowX withY:minLowY withRect:&rect]];
    }
}

- (ChartCanvasTextAnchor) highLowAnchorWithX:(CGFloat)x withY:(CGFloat)y withRect:(Rectangle *) rect
{
    ChartCanvasTextAnchor anchor;
    CGFloat canvasWidthHalf = rect->getCanvasWidth()*0.5, canvasHeightHalf = rect->getCanvasHeight()*0.5;
    //右
    if (x >= canvasWidthHalf) {
        // 下
        if (y >= canvasHeightHalf) {
            anchor = RightMiddle;
        }
        // 上
        else{
            anchor = RightMiddle;
        }
    }
    //左
    else
    {
        // 下
        if (y >= canvasHeightHalf) {
            anchor = LeftMiddle;
        }
        // 上
        else{
            anchor = LeftMiddle;
        }
    }
    
    return anchor;
}

@end
